#!/bin/bash
file_name=functions.sh
funcs=`which $file_name`
if [ "$funcs" != "" ] ;then
  echo "funcs=$funcs"
  . $funcs
else
  funcs=`dirname $0`/$file_name
  echo "$PWD"
  echo "funcs=$funcs"
  if [ "$funcs" != "" ] ;then
    . $funcs
  else
    exit
  fi
fi
machinenames="compiler101,compiler102,compiler103,compiler104,compiler105,compiler106,compiler107,compiler108,compiler109,compiler110,compiler111,compiler112,compiler113,compiler114,compiler115,compiler116,compiler010"


while test $# != 0
do
    case $1 in
	-m)
	    shift
	    mode=$1
	    ;; 
	--config)
	    shift
	    config=$1
	    ;; 
	--machine)
	    shift
	    machine=$1
	    ;; 
        *)
	    cmd=$1
	   ;;
       esac
       shift
done

if [ -z "$config" ]; then
  echo "没有配置文件"
  exit
fi
if [ -n "$cmd" ]; then
  result=`echo "$cmd" |grep ^sudo `
  cmd=`echo $cmd|sed "s/\<^.sudo\>//"`
  if [ -n "$result" ]; then
    mode=sudo
  fi
else
  echo "$0 cmd"
  exit
fi
echo "cmd=$cmd"
if [ -n "$machine" ]; then
  machinenames="$machine"
fi
machinenames="`echo $machinenames |tr ',' ' '`"
for m in $machinenames
do
  hostip=`parseini.shc $config  get $m hostip`
  username=`parseini.shc $config  get $m username`
  password=`parseini.shc $config  get $m password`
  if [ "$mode" = "sudo" ]; then
    echo "执行sudo命令"
    aaa="`ssh-shell-sudo.sh $hostip $username $password \"$cmd\"`"
  else
    echo "执行普通命令"
    aaa="`ssh-shell.sh $hostip $username $password \"$cmd\"`"
  fi
  echoTextBlue "主机 $hostip 上执行命令 $cmd"
  echo "result=$aaa"
done
